import 'package:flutter/material.dart';
import 'package:trip_flutter/util/screen_adapt_helper.dart';
import 'package:url_launcher/url_launcher.dart';

//数据
Map<String, List<String>> LocalDatas = {
  '百度': ['images/BaiDu.png', 'https://www.baidu.com'],
  '阿里': ['images/Ali.png', 'https://www.1688.com/'],
  '腾讯': ['images/Tencent.png', 'https://www.qq.com/'],
  '字节': ['images/ByteDance.png', 'https://www.bytedance.com/'],
  '联想': ['images/LX.png', 'https://www.lenovo.com.cn/'],
};

class localWidget extends StatelessWidget {
  const localWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80.px,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(6)),
      ),
      child: Padding(padding: EdgeInsets.all(6), child: Items()),
    );
  }

  Widget? Items() {
    //一个items列表
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: _Items(),
    );
  }

  List<Widget> _Items() {
    List<Widget> widgets = [];
    for (var key in LocalDatas.keys) {
      String imagePath = LocalDatas[key]![0];
      var widget = GestureDetector(
        onTap: () => _onTap(key),
        child: Column(
          children: [Image.asset(imagePath, height: 45, width: 45), Text(key)],
        ),
      );
      widgets.add(widget);
    }
    return widgets;
  }

  void _onTap(String key) async {
    // 获取原始URL
    String url = LocalDatas[key]![1];
    // 确保URL包含协议头（http/https）
    if (!url.startsWith(RegExp(r'^https?://'))) {
      url = 'https://$url'; // 自动补全https协议
    }
    var uri = Uri.parse(url);
    //跳转到对应的连接处
    if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) {
      throw '无法启动$uri';
    }
  }
}
